<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col
        :xs="24"
        :sm="24"
        :md="8"
        :lg="6"
        :xl="5"
        style="margin-bottom: 10px"
      >
        <el-card class="box-card">
          <div slot="header" class="head">个人信息</div>
          <div class="main-user">
            <div class="avatar">
              <!-- <my-upload
                @crop-success="cropSuccess"
                @crop-upload-success="cropUploadSuccess"
                @crop-upload-fail="cropUploadFail"
                v-model="show"
                :width="200"
                :height="200"
                img-format="png"
                :size="size"
                langType="zh"
                :noRotate="false"
                field="file"
                :url="uploadUrl"
                :headers="headers"
              ></my-upload> -->
              <el-upload
                class="avatar-uploader"
                :show-file-list="false"
                :on-success="handleSuccess"
                :on-error="handleError"
                :headers="headers"
                :action="uploadUrl"
              >
                <img
                  class="imgU"
                  :src="fileName"
                  alt="#"
                  title="点击修改头像"
                />
              </el-upload>
            </div>
            <ul class="user-info">
              <li>
                <div style="height: 100%">
                  <svg-icon icon-class="login" /> 登录账号

                  <div class="user-right">{{ infoData.loginName }}</div>
                </div>
              </li>
              <li>
                <svg-icon icon-class="user1" /> 用户名
                <div class="user-right">{{ infoData.userName }}</div>
              </li>
              <!-- <li>
                <svg-icon icon-class="user1" /> 登录名
                <div class="user-right">{{ infoData.loginName }}</div>
              </li> -->
              <li>
                <svg-icon icon-class="dept" /> 所属部门
                <div
                  class="user-right"
                  :key="item.id"
                  v-for="item in infoData.deptList"
                >
                  {{ item.name + "," }}
                </div>
              </li>
              <li>
                <svg-icon icon-class="phone" /> 手机号码
                <div class="user-right">{{ infoData.phone }}</div>
              </li>
              <li>
                <svg-icon icon-class="email" /> 用户邮箱
                <div class="user-right">{{ infoData.email }}</div>
              </li>
              <li>
                <svg-icon icon-class="anq" /> 安全设置
                <div class="user-right">
                  <a @click="editPassword" style="padding-right: 1rem"
                    >修改密码</a
                  >
                  <a @click="editEmail">修改邮箱</a>
                </div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="16" :lg="18" :xl="19">
        <!-- 密码修改 -->
        <el-dialog
          title="密码修改"
          :visible.sync="dialogVisible"
          width="30%"
          
        >
          <el-form
            label-width="80px"
            :model="editData"
            :rules="rules"
            ref="ruleForm"
          >
            <el-form-item label="原密码：" prop="oldPassword">
              <el-input
                type="password"
                v-model="editData.oldPassword"
              ></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input
                type="password"
                v-model="editData.newPassword"
              ></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPassword">
              <el-input
                type="password"
                v-model="editData.checkPassword"
              ></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="rest">取 消</el-button>
            <el-button type="primary" @click="sureEit">确 定</el-button>
          </span>
        </el-dialog>
        <!-- 邮箱修改 -->
        <el-dialog
          title="密码修改"
          :visible.sync="dialogVisible1"
          width="30%"
         
        >
          <el-form
            label-width="80px"
            :model="editMail"
            :rules="rules"
            ref="ruleForm"
          >
            <el-form-item label="邮箱" prop="email">
              <el-input type="text" v-model="editMail.email"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="rest1">取 消</el-button>
            <el-button type="primary" @click="sureEit1">确 定</el-button>
          </span>
        </el-dialog>

        <!-- 个人信息修改 -->
        <el-dialog
          title="个人信息修改"
          :visible.sync="dialogVisible2"
          width="30%"
          
        >
          <el-form :rules="rules2" label-width="80px" :model="infoData">
            <el-form-item label="用户名：">
              <el-input v-model="infoData.userName"></el-input>
            </el-form-item>
            <el-form-item label="登录名：">
              <el-input v-model="infoData.loginName"></el-input>
            </el-form-item>

            <el-form-item label="手机号：">
              <el-input v-model="infoData.phone"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible2 = false">取 消</el-button>
            <el-button type="primary" @click="sureEit">确 定</el-button>
          </span>
        </el-dialog>
        <!--    用户资料    -->
        <el-card class="box-card">
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="用户资料" name="first">
              <el-form
                ref="form"
                class="form_left"
                :model="form"
                :rules="rules2"
                style="margin-top: 10px"
                size="small"
                label-width="65px"
              >
                <el-form-item label="用户名" prop="nickName">
                  <el-input v-model="form.userName" style="width: 35%" />
                  <span style="color: #c0c0c0; margin-left: 10px"
                    >用户昵称不作为登录使用</span
                  >
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                  <el-input v-model="form.phone" style="width: 35%" />
                  <span style="color: #c0c0c0; margin-left: 10px"></span>
                </el-form-item>

                <el-form-item label="">
                  <el-button
                    :loading="saveLoading"
                    size="mini"
                    type="primary"
                    @click="doSubmit"
                    >保存配置</el-button
                  >
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <!--    操作日志    -->
            <el-tab-pane label="操作日志" name="second">
              <el-table
                v-loading="loading"
                :data="tableData"
                style="width: 100%"
                max-height="450"
              >
                <el-table-column
                
                  type="index"
                  width="50"
                  label="序号"
                >
                </el-table-column>

                <!-- <el-table-column fixed="left" prop="id" label="ID"> </el-table-column> -->
                <!-- <el-table-column prop="method" label="method"> </el-table-column> -->
           
                <!-- <el-table-column prop="queryString" label="queryString">
        </el-table-column> -->
                <!-- <el-table-column prop="requestBody" label="requestBody">
          
        </el-table-column> -->
                <!-- <el-table-column width="150" label="responseBody">
                  <template slot-scope="scope">
                    <el-button
                      icon="el-icon-info"
                      type="success"
                      size="mini"
                      @click="detail(scope.row.responseBody)"
                      >查看详情</el-button
                    >
                  </template>
                </el-table-column>
                <el-table-column
                  width="150"
                  prop="remoteHost"
                  label="remoteHost"
                >
                </el-table-column> -->
                <el-table-column prop="logId" label="日志ID">
                </el-table-column>
                <el-table-column prop="method" label="请求方式">
                </el-table-column>
                <el-table-column prop="uri" label="请求地址">
                </el-table-column>
            
                <el-table-column prop="state" label="操作说明">
                </el-table-column>
                <el-table-column prop="createTime" label="操作时间">
                </el-table-column>
                <el-table-column prop="host" label="用户主机">
                </el-table-column>
                <!-- <el-table-column prop="protocol" label="protocol">
                </el-table-column>
                <el-table-column
                  width="400"
                  prop="requestURL"
                  label="requestURL"
                >
                </el-table-column>
                <el-table-column width="400" label="headers">
                  <template slot-scope="scope">
                    <el-button
                      icon="el-icon-info"
                      type="success"
                      size="mini"
                      @click="detail2(scope.row.headers)"
                      >查看详情</el-button
                    >
                  </template>
                </el-table-column> -->

                <!-- <el-table-column fixed="right" label="操作" width="200px">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column> -->
              </el-table>
              <!--分页组件-->
              <div class="block">
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="this.pageNum.currentPage"
                  :page-sizes="[10, 20, 30, 50]"
                  :page-size="this.pageNum.perPage_size"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="this.pageNum.total"
                >
                </el-pagination>
              </div>
        
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "../../utils/axios";
import md5 from "js-md5";
import eventBus from "../../utils/eventBus";
// import "babel-polyfill"; // es6 shim
// import myUpload from "vue-image-crop-upload/upload-2.vue";

export default {
  // components: { "my-upload": myUpload },
  created() {
    this.getInfo();
    this.getLogList(this.pageNum);
  },

  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.editData.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      detailDialogVisible: false,
      fileName: "",
      fileName2: "",

      rules: {
        oldPassword: [
          { required: true, message: "请输入原密码", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
        ],
        checkPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { validator: validatePass2, trigger: "blur", required: true },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
      },

      rules2: {
        phone: [
          {
            pattern: /^1[3|4|5|7|8][0-9]\d{8}$/,
            message: "手机号格式错误",
            trigger: ["blur", "change"],
          },
        ],
      },

      pageNum: {
        current: 1,
        size: 10,
        total: 0,
      },
      how: false,
      infoData: {},
      editData: {},
      editMail: {},
      userImg: "",

      show: false,

      activeName: "first",
      saveLoading: false,
      loading: true,
      size: 2.1,
      uploadUrl: "http://192.168.72.100:8000/minio/upload",
      headers: {
        Authorization: window.localStorage.getItem("token"),
      },
      tableData: [],
      detailData: {},
      form: {
        userName: "",
        phone: "",
      },
    };
  },
  methods: {
    getInfo() {
      axios({
        method: "GET",
        url: "/sys/user/getLoginUser",
      }).then((response) => {
        if (response.code === 200) {
          this.infoData = response.data;

          this.form.userName = this.infoData.userName;
          this.form.phone = this.infoData.phone;
          if(response.data.photo){
             this.downImg(response.data.photo)
          }else{
            this.fileName='../../assets/imgs/default.jpg'
     
          }
         
        }
      });
    },
    editPassword() {
      this.dialogVisible = true;
      this.editData = {};
    },
    editEmail() {
      this.dialogVisible1 = true;
      this.editMail = {};
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    //密码修改
    sureEit() {
      let obj = {};
      obj.newPassword = md5(this.editData.newPassword);
      obj.oldPassword = md5(this.editData.oldPassword);
      this.$refs.ruleForm.validate((vaild) => {
        if (vaild) {
          axios({
            method: "POST",
            url: "/sys/user/updatePassword",
            data: obj,
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success("密码已修改，请重新登录");
              this.$router.push("/login");
            } else {
              this.$message.error(res.message);
            }
          });
        } else {
          this.$message.error("内容有误，请重新填写");
        }
      });
    },
    rest() {
      this.dialogVisible = false;
    },
    //邮箱修改
    sureEit1() {
      this.editMail.id = this.infoData.id;

      axios({
        method: "PUT",
        url: "/sys/user",
        data: this.editMail,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("修改成功");
          this.getInfo();
          this.dialogVisible1 = false;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    rest1() {
      this.dialogVisible1 = false;
    },
    //个人信息
    editInfo() {
      this.dialogVisible2 = true;
    },
    handleClick(tab, event) {
      console.log(event);
      if (tab.name === "second") {
        console.log(1);
      }
    },
    doSubmit() {
      this.form.id = this.infoData.id;

      axios({
        method: "PUT",
        url: "/sys/user",
        data: this.form,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("修改成功");
          this.getInfo();
          this.dialogVisible1 = false;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    handleSuccess(res, file) {
      if (res.code == 200) {
        this.$notify({
          title: "头像上传成功",
          type: "success",
          duration: 1000,
        });

    
        this.fileName2 = res.data.url;
       
           let obj = {};
          obj.photo = this.fileName2;
          obj.id = this.infoData.id;
          axios({
            method: "PUT",
            url: "/sys/user",
            data: obj,
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success("修改成功");
              this.getInfo();
            } else {
              this.$message.error(res.message);
            }
          });
     
    
        // eventBus.$emit("touxiang", this.fileName);
      }
      console.log(file);
   
    },
    //上传成功以及调用组件,图片下载模块更新用户头像
    downImg(con) {
      axios({
        method: "post",
        url: "/minio/download",
        headers: {
          "Content-Type": "application/json",
          // EngineCode: "j371gy1q1ioeveq2y05l4gjz3",
          // EngineSecret:
          //   "IgMlZo+KOs8FthII3KnobUoynILH5ELJVqcHNZcs1G2mwrkofR1THw==",
        },
        responseType: "blob",
        data: {
          fileName: con,
        },
      })
        .then((res) => {
         
          //修改信息
          let url111 =   window.URL.createObjectURL(res)
        
          this.fileName = url111;
       
          eventBus.$emit("touxiang", this.fileName);
       
        })
        .catch((e) => {
          throw e;
        });
    },


    // 监听上传失败
    handleError(e) {
      const msg = JSON.parse(e.message);
      this.$notify({
        title: msg.message,
        type: "error",
        duration: 2500,
      });
    },
    //控制剪切框的显示和隐藏,头像上传
    toggleShow() {
      this.show = !this.show;
    },
    cropSuccess(imgDataUrl, field) {
      //接口请求
      console.log("-------- crop success --------", imgDataUrl, field);
    },
    //上传成功回调
    cropUploadSuccess(jsonData, field) {
      console.log("-------- upload success --------");

      console.log(jsonData);
      console.log("field: " + field);
    },
    //上传失败回调
    cropUploadFail(status, field) {
      console.log("-------- upload fail --------");
      console.log("上传失败状态" + status);
      console.log("field: " + field);
    },
    //查询日志
    getLogList(con) {
      axios({
        method: "GET",
        url: "/sys/user/log",
        params: con,
      }).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.records;
          this.pageNum.current = res.data.current;
          this.pageNum.size = res.data.size;
          this.pageNum.total = res.data.total;
          this.loading = false;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    //详情
    detail(con) {
      this.detailDialogVisible = true;
      this.detailData = con;
    },
    detail2(con) {
      this.detailDialogVisible = true;
      this.detailData = con;
    },
    delmenuhandleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    //页码
    handleSizeChange(val) {
      this.pageNum.size = val;

      this.getLogList(this.pageNum);
    },
    handleCurrentChange(val) {
      this.pageNum.current = val;

      this.getLogList(this.pageNum);
    },
  },
};
</script>

<style scoped lang="less">
.head {
  text-align: left;
  font-size: 1.76rem;
}
.main-user {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.avatar {
  width: 120px;
  height: 120px;
  border: 0.1rem solid #ccc;
  box-shadow: rgb(110, 109, 109) 0 0 1.5rem;
  border-radius: 50%;

  img {
    width: 100%;
    height: 100%;
  }
}
.user-info {
  padding-left: 0;
  list-style: none;
  width: 100%;

  li {
    width: 100%;
    display: list-item;
    text-align: left;
    border-bottom: 1px solid #f0f3f4;
    padding: 11px 0;
    font-size: 13px;
  }
  .user-right {
    float: right;
    a {
      color: #317ef3;
    }
  }
}
.form_left {
  text-align: left;
}
.block {
  margin-top: 25px;
  float: left;

}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader {
  width: 100%;
  height: 100%;

  .imgU {
    width: 118px;
    height: 118px;
    border-radius: 50%;
  }
}
.el-table /deep/ {
  font-size: 12px;
  padding: 2rem 0 0 2rem;
  overflow: visible;

}

.el-table /deep/ thead {
   font-size: 12.5px;
    color: #909399;
    font-weight: 700;
}
.el-table::before {
    z-index: inherit;
    visibility: hidden;
}


</style>
